// 简化的API接口，直接调用后端服务
const API_BASE_URL = 'http://localhost:8080';

// 获取所有学生
export const getAllStudents = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students`);
    if (!response.ok) throw new Error('获取学生列表失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生列表错误:', error);
    throw error;
  }
};

// 根据ID获取学生
export const getStudentById = async (id) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/${id}`);
    if (!response.ok) throw new Error('获取学生信息失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生信息错误:', error);
    throw error;
  }
};

// 根据学号获取学生
export const getStudentByStudentId = async (studentId) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/student-id/${studentId}`);
    if (!response.ok) throw new Error('获取学生信息失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生信息错误:', error);
    throw error;
  }
};

// 搜索学生
export const searchStudents = async (keyword) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students?keyword=${encodeURIComponent(keyword)}`);
    if (!response.ok) throw new Error('搜索学生失败');
    return await response.json();
  } catch (error) {
    console.error('搜索学生错误:', error);
    throw error;
  }
};

// 根据姓名获取学生
export const getStudentsByName = async (name) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/name/${encodeURIComponent(name)}`);
    if (!response.ok) throw new Error('获取学生列表失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生列表错误:', error);
    throw error;
  }
};

// 根据专业获取学生
export const getStudentsByMajor = async (major) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/major/${encodeURIComponent(major)}`);
    if (!response.ok) throw new Error('获取学生列表失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生列表错误:', error);
    throw error;
  }
};

// 根据性别获取学生
export const getStudentsByGender = async (gender) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/gender/${encodeURIComponent(gender)}`);
    if (!response.ok) throw new Error('获取学生列表失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生列表错误:', error);
    throw error;
  }
};

// 按GPA排序获取学生
export const getStudentsSortedByGpa = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/sorted-by-gpa`);
    if (!response.ok) throw new Error('获取学生列表失败');
    return await response.json();
  } catch (error) {
    console.error('获取学生列表错误:', error);
    throw error;
  }
};

// 创建学生
export const createStudent = async (studentData) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(studentData),
    });
    if (!response.ok) throw new Error('创建学生失败');
    return await response.json();
  } catch (error) {
    console.error('创建学生错误:', error);
    throw error;
  }
};

// 更新学生
export const updateStudent = async (id, studentData) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/${id}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(studentData),
    });
    if (!response.ok) throw new Error('更新学生失败');
    return await response.json();
  } catch (error) {
    console.error('更新学生错误:', error);
    throw error;
  }
};

// 删除学生
export const deleteStudent = async (id) => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/${id}`, {
      method: 'DELETE',
    });
    if (!response.ok) throw new Error('删除学生失败');
    return true;
  } catch (error) {
    console.error('删除学生错误:', error);
    throw error;
  }
};

// 获取统计信息
export const getStatistics = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/api/students/statistics`);
    if (!response.ok) throw new Error('获取统计信息失败');
    return await response.json();
  } catch (error) {
    console.error('获取统计信息错误:', error);
    throw error;
  }
};